package com.hmy.uaa.controller.api;

import com.hmy.uaa.bo.request.ResourceRequest;
import com.hmy.uaa.bo.request.RoleResourceRequest;
import com.hmy.uaa.bo.response.ResourceResponse;
import com.hmy.uaa.service.ResourceService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.groups.Default;
import java.util.List;


@ApiIgnore
@Validated
@RestController
@RequestMapping("/api/resource")
@Slf4j
public class ResourceController {

    @Autowired
    private ResourceService resourceService;


    @PostMapping
    @ApiOperation("新增资源")
    public void add(@RequestBody @Validated({ResourceRequest.Insert.class, Default.class}) ResourceRequest resourceRequest) {
        log.info(resourceRequest.toString());
        resourceService.save(resourceRequest);
    }


    @ApiOperation("删除资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id")
    })
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        log.info("id:{}", id);
        resourceService.delete(id);

    }

    @ApiOperation("查询资源列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单id")
    })
    @GetMapping("/list")
    public List<ResourceResponse> queryResourceList(
            @RequestParam(value = "menuId", required = false) Long menuId) {

        return resourceService.queryResourceList(menuId);
    }

    @ApiOperation("绑定角色")
    @PostMapping("/role")
    public void bindRole(@RequestBody @Validated({Default.class}) RoleResourceRequest roleResourceRequest) {
        log.info(roleResourceRequest.toString());
        resourceService.bindRole(roleResourceRequest);
    }


    @ApiOperation("查询绑定资源列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id")
    })
    @GetMapping("/bind/list")
    public List<ResourceResponse> queryBindResourceList(
            @RequestParam(value = "roleId", required = false) Long roleId) {

        return resourceService.queryBindResourceList(roleId);
    }
}
